package service

import (
	"app/internal/struct/web"
	"app/util"
	"errors"
)

func (s *Service) IsVaildLicense(li *web.Register) error {

	if s.cf.License.Code == li.Code {
		return nil
	} else {
		return errors.New(licenseFaildString)
	}
	return nil
}

func (s *Service) CreateUUID(li *web.Register) error {

	if li.Name == "" {
		li.Id = util.NewUUID()
		return nil
	} else {
		li.Id = util.NewNameUUID(li.Name)
		return nil
	}
	li.Id = util.NewUUID()

	return nil
}

func (s *Service) SaveLicense(li *web.Register) error {

	license := web.License{Id: li.Id, Licensecode: li.Code}
	s.dao.License(license)

	util.Info("id %s", li.Id)

	s.dao.SetLicenseId(li.Id, li.Code)

	if err := s.dao.FindId(li.Id); err == nil {
		return nil
	}

	return errors.New("store faild")
}

func (s *Service) CreateLicenseCode(li *web.Register) error {

	code, err := s.dao.GetLicenseCode(li.Id, util.GetNowUnixSec(), s.cf.License.Validate)
	if err != nil {
		return err
	}
	li.Code = code
	return nil
}

func (s *Service) SyncId(id string) error {

	license := web.SyncLicense{Id: id}
	return s.dao.SyncLicense(license)
}

func (s *Service) Heart(id string) error {

	s.dao.UserHeart(id)

	license := web.Heart{Id: id, Time: util.GetNowUnixSec()}
	return s.dao.Heart(license)
}

func (s *Service) RefreshUserInfo(obj interface{}) error {

	return s.dao.User(obj)
}

func (s *Service) RegisterUser(obj interface{}) error {

	return s.dao.Register(obj)
}

func (s *Service) IsExitsUserId(id string) error {

	if err := s.dao.FindId(id); err != nil {
		return err
	}
	return nil
}
